﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Dal
{
    public class Tools
    {
        public static DataSet getDistributorsList(string ambient)
        {
            string cnx = Credentials.getCredentias(ambient);
            if (cnx == null) return null;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(cnx);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dms_Report_DistributorsList";
            cmd.CommandTimeout = 0;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("dtDistributorsList");

            try
            {
                cmd.Connection.Open();
                da.Fill(dt);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("getDistributorsList: " + ex.Message);
                return null;
            }
            DataSet ds = new DataSet("dsDistributorsList");
            ds.Tables.Add(dt);
            return ds;
        }

        public static DataSet getCountryList(string ambiente) {

            string cnx = Credentials.getCredentias(ambiente);
            if (cnx == null) return null;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(cnx);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dms_Get_Country";
            cmd.CommandTimeout = 0;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("dtCountryList");
            try
            {
                cmd.Connection.Open();
                da.Fill(dt);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("getCountryList: " + ex.Message);
                return null;
            }
            DataSet ds = new DataSet("dsCountryList");
            ds.Tables.Add(dt);
            return ds;
        }

        /*DISTRIBUIDORES CON PAISES*/
        public static DataSet getDistribuitorsListWhitCountries()
        {

            string cnx = Credentials.getCredentias("DMSTools");
            if (cnx == null) return null;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(cnx);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dms_GetDistribuitorsWithCountries";
            cmd.CommandTimeout = 0;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("dtDistList");
            try
            {
                cmd.Connection.Open();
                da.Fill(dt);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("getDistribuitorsListWhitCountries: " + ex.Message);
                return null;
            }
            DataSet ds = new DataSet("dsDistList");
            ds.Tables.Add(dt);
            return ds;
        }

       

    }
}